// pagesA/waiterspay/waiterspay.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    servicesItem: null,
    type: '',
    serverId: '',
    orderId: Math.round(new Date()),
    addresconter: wx.getStorageSync('addresconter'),
    serviceDetailList: null,
    regionList: [],
    orderNo: null,
    queryFrom: null,
    isLock: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      queryFrom: JSON.parse(options.servicesItem),
      type: options.type,
      serverId: options.serverId,
    })
    this.getInitList()

    if (!app.globalData.userInfo) {
      app.getUserInfo().then(res => {
        this.setData({
          userInfo: res
        })
      })
    } else {
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },
  getInitList() {
    let that = this
    that.setData({
      loading: true
    })
    wx.showLoading({
      mask: true
    })
    wx.request({
      url: app.globalData.serverUrl + '/services/items/detail',
      method: 'POST',
      data: that.data.queryFrom,
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          let newserve = resData.data.servicesItem
          let ner = null
          newserve.serviceDetailList.map(x => {
            if (x.serviceDetailId == that.data.serverId) {
              ner = x
            }
          })
          this.setData({
            servicesItem: resData.data.servicesItem,
            loading: false,
            serviceDetailList: ner
          })
          this.getRegionList()
          wx.hideLoading()
        }
      })
    })
  },
  getRegionList() {
    let that = this
    let newregionList = []
    wx.request({
      url: app.globalData.serverUrl + '/treatment/list',
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.regionList.map(item => {
            if (that.data.servicesItem.treatmentIdList.some(x => {
                return x == item.treatmentId
              })) {
              newregionList.push(item)
            }
          })
          that.setData({
            regionList: newregionList
          })
        }
      })
    })
  },
  bindtapPay() {
    console.log('支付成功');
    let that = this
    let lock=null
    if(!that.data.isLock){
      that.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(that.data.isLock && lock){
      if(lock-that.data.isLock<app.globalData.isNumLock){
        return
      }else{
        that.setData({
          isLock:lock
        })
      }
    }
    wx.showLoading({
      title: '订单生成中',
      icon:'none',
      mask:true
    })
    wx.request({
      url: app.globalData.serverUrl + '/wx/payment',
      method: 'POST',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: {
        orderType: 0,
        regionName: that.data.servicesItem.regionName == null ? wx.getStorageSync('addresconter') : that.data.servicesItem.regionName,
        serviceDetailId: that.data.serverId
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          wx.request({
            url: app.globalData.serverUrl + '/ddOrder/placing',
            method: 'POST',
            header: {
              'token': wx.getStorageSync('token')
            },
            data: {
              orderType: 0,
              regionName: that.data.servicesItem.regionName == null ? wx.getStorageSync('addresconter') : that.data.servicesItem.regionName,
              serviceDetailId: that.data.serverId,
              prepayId: resData.data.package,
              orderNo: resData.data.orderNo
            },
            success: (resEnd => {
              let resEndData = resEnd.data
              if (resEndData.code == 200) {
                wx.requestPayment({
                  timeStamp: resData.data.timeStamp,
                  nonceStr: resData.data.nonceStr,
                  package: resData.data.package,
                  signType: resData.data.signType,
                  paySign: resData.data.paySign,
                  success(res) {
                    wx.hideLoading()
                    wx.redirectTo({
                      url: '/pagesA/waiterspayorder/waiterspayorder?orderNo=' + resData.data.orderNo,
                    })
                  },
                  fail(res) {
                    wx.hideLoading()
                    // wx.request({
                    //   url: app.globalData.serverUrl + '/ddOrder/placing',
                    //   method: 'POST',
                    //   header: {
                    //     'token': wx.getStorageSync('token')
                    //   },
                    //   data: {
                    //     orderType: 0,
                    //     regionName: that.data.servicesItem.regionName,
                    //     serviceDetailId: that.data.serverId,
                    //     prepayId:resData.data.package,
                    //     orderNo:'22222'
                    //   },
                    //   success: (resEnd => {
                    //     let resEndData = resEnd.data
                    //     if (resEndData.code == 200) {
                    //       wx.redirectTo({
                    //         url: '/pagesA/waiterspayorder/waiterspayorder?orderNo=22222',
                    //       })
                    //     }
                    //   })
                    // })
                  }
                })
                
              }
            })
          })
          
        }
      })
    })
    // wx.navigateTo({
    //   url: '/pagesA/waiterspayorder/waiterspayorder',
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      isLock: null
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})